home *** CD-ROM | disk | FTP | other *** search
- unit Demomain;
-
- interface
-
- {Demonstration program using TSQLBuilder component}
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, Buttons, StdCtrls, DB, DBTables, SQLBuild, Grids, DBGrids,
- ExtCtrls;
-
- Const
- MaxDatabases = 4; {Set to whatever you like}
- type
- TForm1 = class(TForm)
- Memo1: TMemo;
- sbBuildSQL: TSpeedButton;
- cboxChangeAlias: TCheckBox;
- cboxUpdateQueries: TCheckBox;
- cboxDeleteQueries: TCheckBox;
- Query1: TQuery;
- DBGrid1: TDBGrid;
- DataSource1: TDataSource;
- Bevel1: TBevel;
- Label2: TLabel;
- Label3: TLabel;
- SQLBuilder1: TSQLBuilder;
- cboxShowSysTables: TCheckBox;
- cboxCanModify: TCheckBox;
- cboxAutoGroupby: TCheckBox;
- Label4: TLabel;
- SpeedButton1: TSpeedButton;
- cbSQLStyle: TComboBox;
- Label5: TLabel;
- rbQueryStartup: TRadioGroup;
- procedure sbBuildSQLClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- procedure SpeedButton1Click(Sender: TObject);
- private
- { Private declarations }
- DBArray : Array[1..(MaxDatabases-1)] of TDatabase;
- StandardDB : TDatabase;
- Procedure SetBuilderOptions;
- Procedure PrepareAndRunQuery;
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
- Uses
- SqlBld08;
- {$R *.DFM}
-
- function Long2Str(L : LongInt) : string;
- {-Convert a long/word/integer/byte/shortint to a string}
- var
- S : string;
- begin
- Str(L, S);
- Long2Str := S;
- end;
-
- Procedure Tform1.SetBuilderOptions;
- Var
- TheOptions:TSQLBuilderOptions;
- begin
- TheOptions := [];
- If cboxChangeAlias.Checked then
- Include(TheOptions, sbAllowChangeAlias);
- If cboxUpdateQueries.Checked then
- Include(TheOptions, sbAllowUpdates);
- If cboxDeleteQueries.Checked then
- Include(TheOptions, sbAllowDeletes);
- If cboxShowSysTables.Checked then
- Include(TheOptions, sbShowSystemTables);
- If cboxCanModify.Checked then
- Include(TheOptions, sbEnableEditor);
- SQLBuilder1.Options := TheOptions;
- SQLBuilder1.AutoGroup := cboxAutoGroupby.Checked;
- SQLBuilder1.UseWizardOnShow := (rbQueryStartup.ItemIndex = 1);
- SQLBuilder1.AutoloadLastQuery := (rbQueryStartup.ItemIndex = 0);;
- SQLBuilder1.SQLType := TSQLType(cbSQLStyle.ItemIndex);
- end;
-
- procedure Tform1.PrepareAndRunQuery;
- {Opens any necessary databases, sets up the TQuery and runs it}
- Var
- ACount :Integer;
- begin
- If SQLBuilder1.QueryType <> SelectQuery then
- MessageDlg('Sorry, for safety reasons the demonstration program will only run "Select" queries.', mtInformation, [mbOk], 0)
- else If SQLBuilder1.UsedAliasList.Count > MaxDatabases then
- MessageDlg('Unable to run this query. The demo progam is limited to '
- +'a maximum of '+Long2Str(MaxDatabases)+' separate databases', mtInformation, [mbOk], 0)
- else
- Try
- screen.Cursor := crHourglass;
-
- {Place first alias in TQuery.Databasename}
- If SQLBuilder1.IsHeterogeneous then
- {use dummy program STANDARD alias as Databasename}
- Query1.DatabaseName := StandardDB.Databasename
- else
- Query1.DatabaseName := SQLBuilder1.UsedAliasList.Strings[0];
-
- {If there is more than one alias involved then open databases for them}
- If SQLBuilder1.IsMultiAlias then
- For aCount := 1 to SQLBuilder1.UsedAliasList.Count-1 do
- Try
- DBArray[aCount].Free;
- DBArray[aCount] := TDatabase.Create(Self);
- DBArray[aCount].Aliasname := SQLBuilder1.UsedAliasList.Strings[aCount];
- DBArray[aCount].Databasename := SQLBuilder1.UsedAliasList.Strings[aCount];
- DBArray[aCount].Name := 'DB'+Long2Str(aCount+1);
- DBArray[aCount].Open;
- except
- On E: EDatabaseError do
- begin
- MessageDlg('Unable to open the database: '+DBArray[aCount].Aliasname
- +#13#10+'Error: '+E.Message, mtError, [mbOk], 0);
- abort;
- end;
- end;
-
- {If we get this far then attempt to run the query}
- If SQLBuilder1.SQLModified then
- Query1.SQL.Assign(SQLBuilder1.UserSQL)
- else
- Query1.SQL.Assign(SQLBuilder1.SQL);
- Query1.Open;
- finally
- screen.Cursor := crDefault;
- end;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- var
- dCount:Integer;
- begin
- {Initialize the Database array}
- For dCount := 1 to (MaxDatabases-1) do
- DBArray[dCount] := Nil;
- {Create a STANDARD type database alias to use with heterogeneous queries}
- StandardDB := TDatabase.Create(Self);
- StandardDB.Databasename := 'SQLBldDemo';
- StandardDB.DriverName:= 'STANDARD';
- StandardDB.Params.Clear;
- StandardDB.Params.Add('PATH='+SQLBuilder1.PrivateDir);
- StandardDB.Open;
- cbSQLStyle.ItemIndex := 3;
- end;
-
- procedure TForm1.FormDestroy(Sender: TObject);
- Var
- dCount:Integer;
- begin
- StandardDB.Free;
- For dCount := 1 to (MaxDatabases-1) do
- DBArray[dCount].Free;
- end;
-
- procedure TForm1.sbBuildSQLClick(Sender: TObject);
- begin
- Query1.Active := False;
-
- {Set SQLBuilder options to match options selected in checkboxes}
- SetBuilderOptions;
-
- {Build the query }
- If SQLBuilder1.Execute then
- begin
- memo1.lines.Assign(SQLBuilder1.SQL);
- PrepareAndRunQuery;
- end;
- end;
-
- procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- CanClose := (MessageDlg('Leave SQLBuilder Demonstration ?', mtConfirmation, [mbYes, mbNo], 0) = mrYes);
- end;
-
- procedure TForm1.SpeedButton1Click(Sender: TObject);
- begin
- Close;
- end;
-
- end.
-